/**
 * Created by win 10 on 2/8/2018.
 */
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
$(function () {
    /* 时间插件 */
    $("#setupTime").click(function () {
        $.jeDate('#setupTime',{
            trigger:false,
            minDate: '1900-1-1',
            isTime:true,
            format:'YYYY-MM-DD'
        })
    })
    $("#registerTime").click(function () {
        $.jeDate('#registerTime',{
            trigger:false,
            minDate: '1900-1-1',
            isTime:true,
            format:'YYYY-MM-DD'
        })
    })
    $("#putOnTime").click(function () {
        $.jeDate('#putOnTime',{
            trigger:false,
            minDate: '1900-1-1',
            isTime:true,
            format:'YYYY-MM-DD'
        })
    })
    $("#fundSubmission").validate({
        rules: {
            fundName: {
                required: true
            },
            foundTime: {
                required: true
            },
            endTime: {
                required: true
            },
            palnMoney: {
                required: true,
                number: true
            },
            realMoney: {
                required: true,
                number: true
            },
            bankrollSource: {
                required: true
            },
            investAspect: {
                required: true
            },
            investCount: {
                required: true,
                isInteger: true
            },
            investMinMoney: {
                required: true,
                number: true
            },
            investAgencyCount: {
                required: true,
                isInteger: true
            },
            agencyRealMoney: {
                required: true,
                number: true
            },
            investNaturalCount: {
                required: true,
                isInteger: true
            },
            naturalRealMoney: {
                required: true,
                number: true
            },
            trusteeshipUser: {
                required: true
            },
            rankRatio: {
                required: true,
                isInteger: true
            }
        },
        messages: {
            investCount: {
                required: '必填',
                digits:'需填正整数'
            },
            investMinMoney: {
                required: '必填',
                number:'需填数字'
            },
            investAgencyCount: {
                required: '必填',
                digits:'需填正整数'
            },
            agencyRealMoney: {
                required: '必填',
                number:'需填数字'
            },
            investNaturalCount: {
                required: '必填',
                digits:'需填正整数'
            },
            naturalRealMoney: {
                required: '必填',
                number:'需填数字'
            }
        },
        // errorPlacement: function (error, element) {
        //
        // },
        errorClass: 'checkerror',
        // debug: true,
        submitHandler: function(form) {
            var params = ($("#fundSubmission").serializeObject());
            console.log(params);
            params.putOnRecord = parseInt(params.putOnRecord);
            params.isTrusteeship = parseInt(params.isTrusteeship);
            params.isRank = parseInt(params.isRank);
            if (GetQueryString("id")) {
                params['id'] = GetQueryString("id");
            }
            $.ajax({
                url: '/sichuansfc/fund/manager/fund/update.bs',
                type: 'post',
                headers: {'FUND_USER_TOKEN': $.cookie('FUND_USER_TOKEN')},
                contentType: 'application/json',
                xhrFields: {withCredentials: true},
                data: JSON.stringify(params),
                success: function (data) {
                    if (data.status === 100000) {
                        console.log(data);
                        alert('信息报送成功');
                        location.href = '../html/fund-success.html?id='+data.data.id
                    } else {
                        alert(data.msg)
                    }
                },
                error: function () {
                    alert("服务器异常，请稍后重试")
                }
            });
        }
    })
})
jQuery.validator.addMethod('isInteger', function (value, element) {
    var integer = /^[0-9]\d*$/;
    return this.optional(element) || (integer.test(value));
}, "请输入正整数")
/*
function navList() {
    /!* 拉取我的基金管理人信息列表和基金信息列表 *!/
    $.ajax({
        url: '/sichuansfc/fund/manager/profile.bs',
        type: 'get',
        headers: {'FUND_USER_TOKEN': $.cookie('FUND_USER_TOKEN')},
        success: function (data) {
            if (data.status === 100000) {
                for (var i = 0; i < data.data.length; i++) {
                    if ($(".fund-manager")) {
                        $(".fund-manager").append(
                            '<option value="' + data.data[i].id + '">' + data.data[i].managerName + '</option>'
                        )
                    }
                    if ($(".admin-all")) {
                        $(".admin-all").append(
                            '<li><a href="admin-success.html?id='+data.data[i].id+'">' + data.data[i].managerName + '</a></li>'
                        )
                        for (var j = 0; j < data.data[i].funds.length; j++) {
                            $(".fund-all").append(
                                '<li><a href="fund-success.html?id='+data.data[i].funds[j].id+'">' + data.data[i].funds[j].fundName + '</a></li>'
                            )
                        }
                    }

                }
                location.href = '../html/fund-success.html?id='+data.data[0].id
            }
        }
    })
}*/
